package com.heima.article.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.IApCollectionService;
import com.heima.model.article.dtos.ApCollectionDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.AppThreadLocalUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-05
 */
@Service
@AllArgsConstructor
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements IApCollectionService {


    private final ApCollectionMapper apCollectionMapper;

    @Override
    public ResponseResult insert(ApCollectionDto dto) {
        if (dto.getOperation() == 0){
            ApCollection apCollection = new ApCollection();
            BeanUtils.copyProperties(dto,apCollection);
            apCollection.setCollectionTime(LocalDateTime.now());
            apCollection.setArticleId(Long.valueOf(dto.getEntryId()));
            apCollection.setEntryId(AppThreadLocalUtil.getUser());
            apCollectionMapper.insert(apCollection);
        }else {
            LambdaQueryWrapper<ApCollection> wrapper =  new LambdaQueryWrapper<>();
            wrapper.eq(ApCollection::getEntryId,AppThreadLocalUtil.getUser());
            wrapper.eq(ApCollection::getArticleId,dto.getEntryId());
            apCollectionMapper.delete(wrapper);
        }
        return ResponseResult.okResult("");
    }
}
